
const app = getApp()
const $api = require('../../utils/api.js').API;
var login = require('../../utils/login.js');
Page({
    data: {
        page: 1,
        noneShow:false,
        select_true1:true,
        select_true2:false,
        select_true3:false,
        select_true4:false,
        list:{},
        dataType:1,//商品
        pageShow:true,
        imgUrl:app.globalData.imgUrl,
    },
    onShow:function(){
        //wx.setStorageSync('uid',12)
        this.login()
    },
    getlist: function(){
        var that = this
        var data ={
            'uid':wx.getStorageSync('uid'),
            'type':this.data.dataType,
            'page':this.data.page
        }
        var list = this.data.list
        $api.get('collection/list',data).then(res => {
            if(res.data.data.total == 0){
                that.setData({
                    noneShow:true
                })
            }else{
                that.setData({
                    noneShow:false
                })
            }
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },
    selectCate:function(e){
        var ii = e.currentTarget.dataset.index;
        //wx.setStorageSync("role",2)
        if(ii == 3 || ii == 4){
            if(wx.getStorageSync("role") !== 3){
                wx.showToast({
                    title:'暂无权限!!!',
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
                return;
            }
        }
        var type = 'select_true'+ii
        this.setData({
            select_true1:false,
            select_true2:false,
            select_true3:false,
            select_true4:false,
            dataType:ii,
            page:1,
            pageShow:true
        });
        this.setData({
            [type]:true,
        });
        wx.showLoading()
        this.getlist();
    },
    info:function(e){
        if(wx.getStorageSync("role") == 1){
            wx.showToast({
                title:'暂未注册!!!',
                duration:2000,//显示时长
                mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon:'none', //图标，支持"success"、"loading" 
                success: function(a){
                    wx.navigateTo({
                        url: "/pages/login/index"
                    })
                }
            })
            return;
        }
        var id = e.currentTarget.dataset.id;
        var url ='';
        if(this.data.dataType == 1){
            var url = "/pages/pointsMall/info?id="+id
        }
        if(this.data.dataType == 2){
            var url = "/pages/company/info?id="+id
        }
        if(this.data.dataType == 3){
            var url = "/pages/job/info?id="+id
        }
        if(this.data.dataType == 4){
            var url = "/pages/factory/info?id="+id
        }
        wx.navigateTo({
            url:url
        })
    },
    onShareAppMessage: function() {
        return {
            title: "天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
      },
    onReachBottom: function(t){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            wx.showLoading()
            this.getlist();
        }
    },
    delete:function(e){
        var id = e.currentTarget.dataset.id;
        var that = this;
        var data = {'id':id}
        $api.delete('collection/delete',data).then(res =>{
            //请求成功
            if(res.data.code == 1){
                wx.showToast({
                    title:'删除成功!!!',
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
                that.setData({
                    page:1
                })
                wx.showLoading()
                that.getlist();
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },

    login:function(){
        if(!wx.getStorageSync('uid')){
          //没有缓存 跳转授权页面
          wx.navigateTo({
            url:'/pages/index/guide'
          })
        }else{
          //验证手机号码是否存在
          this.checkphone();
        }
      },
      //查询手机号  是否注册
      checkphone:function(){
        var data = {
          'uid':wx.getStorageSync('uid')
        }
        var that = this
        $api.post('member/phone',data).then(res =>{
          if(res.data.code == 1){
            wx.showLoading()
            that.getlist()
          }else{
            wx.navigateTo({
              url:'/pages/index/phone'
            })
          }
        })
      }
});